package com.ghsc.ghxc.jsb.config

import com.ghsc.ghxc.jsb.ui.bean.SysConfigResult
import com.ghsc.ghxc.jsb.ui.integral.bean.IntegralUserInfoData
import com.ghsc.ghxc.jsb.ui.live.bean.AppLiveTaskData
import com.ghsc.ghxc.jsb.ui.my.bean.UserBeanData
import com.google.gson.Gson
import com.tencent.mmkv.MMKV

/**
 * 创建人 @author： xiaolin
 * 描述 ：Todo...
 * 缓存
 */
object UserManager {
    /**退出**/
    fun loginOut() {
        MMKV.defaultMMKV().removeValuesForKeys(
            arrayOf(
                SP_USER_INFO,
                USER_ID,
                USER_R_ID,
                SP_CONFIG,
                USER_NAME,
                USER_HEAD,
                APP_NOTIE,
                APP_UP_VERSION,
                SP_USER_INFO_IN,
                APP_TASK,
                APP_TASK_TIME,
                IM_COUNT

        )
        )
    }
    //融云系统未读消息数量
    private  const val IM_COUNT ="im_count"
    private const val SP_USER_INFO = "sp_user_info" //用户信息
    private const val SP_USER_INFO_IN = "sp_user_info_in" //用户信息
    private const val USER_HEAD = "userHead" //用户头像
    private const val USER_NAME = "userName" //用户昵称
    private const val SP_CONFIG = "sp_user_config" //配置
    private const val APP_NOTIE = "Notice" //公告 一天一次
    //用户信息
    private const val APP_IS_FIRST = "IS_FIRST" //是否第一次打开,显示协议
    private const val USER_ID = "userID" //用户id
    private const val USER_R_ID = "userRID" //用户rid
    private const val APP_UP_VERSION = "app_version" // 1天显示1次

    private const val APP_TASK = "task_live" //直播任务
    private const val APP_TASK_TIME = "task_live_time" //直播任务

    fun setIsFirst(name:String){
        MMKV.defaultMMKV().encode(APP_IS_FIRST, name ?: "")
    }
    fun getIsFirst(): String {
        return MMKV.defaultMMKV().decodeString(APP_IS_FIRST, "") ?: ""
    }

    fun setUserId(id:String){
        MMKV.defaultMMKV().encode(USER_ID, id ?: "")
    }
    fun getUserId(): String {
        return MMKV.defaultMMKV().decodeString(USER_ID, "") ?: ""
    }
    fun setUserName(name:String){
        MMKV.defaultMMKV().encode(USER_NAME, name ?: "")
    }
    fun getUserName(): String {
        return MMKV.defaultMMKV().decodeString(USER_NAME, "") ?: ""
    }
    fun setUserHead(head:String){
        MMKV.defaultMMKV().encode(USER_HEAD, head ?: "")
    }
    fun getUserHead(): String {
        return MMKV.defaultMMKV().decodeString(USER_HEAD, "") ?: ""
    }
    fun setUserRId(id:String){
        MMKV.defaultMMKV().encode(USER_R_ID, id ?: "")
    }
    fun getUserRId(): String {
        return MMKV.defaultMMKV().decodeString(USER_R_ID, "") ?: ""
    }
    fun setImCount(name:Int){
        MMKV.defaultMMKV().encode(IM_COUNT, name ?: 0)
    }
    fun getImCount(): Int {
        return MMKV.defaultMMKV().decodeInt(IM_COUNT, 0) ?: 0
    }
    fun setAppNotice(name:String){
        MMKV.defaultMMKV().encode(APP_NOTIE, name ?: "")
    }
    fun getAppNotice(): String {
        return MMKV.defaultMMKV().decodeString(APP_NOTIE, "") ?: ""
    }

    fun setAppVersionTime(time: String){
        MMKV.defaultMMKV().encode(APP_UP_VERSION, time ?: "")
    }
    fun getAppVersionTime(): String {
        return MMKV.defaultMMKV().decodeString(APP_UP_VERSION, "") ?: ""
    }


    fun setLiveTaskTime(time: Int){
        MMKV.defaultMMKV().encode(APP_TASK_TIME, time ?: 0)
    }
    fun getLiveTaskTime(): Int {
        return MMKV.defaultMMKV().decodeInt(APP_TASK_TIME, 0) ?: 0
    }

    fun setLiveTask(bean: AppLiveTaskData?){
        if (bean == null) {
            return
        }
        val newUserInfoStr = Gson().toJson(bean)
        val oldUserInfoStr = MMKV.defaultMMKV().decodeString(APP_TASK, "") ?: ""
        if (newUserInfoStr == oldUserInfoStr) {
            return
        }
        MMKV.defaultMMKV().encode(APP_TASK, newUserInfoStr)
    }
    fun getLiveTask(): AppLiveTaskData? {
        val userInfoStr = MMKV.defaultMMKV().decodeString(APP_TASK, "") ?: ""
        return try {
            Gson().fromJson(userInfoStr, AppLiveTaskData::class.java)
        } catch (e: java.lang.Exception) {
            null
        }
    }



    /**保存配置信息**/
    fun saveConfig(configResult: SysConfigResult?) {
        if (configResult == null) {
            return
        }
        val newUserInfoStr = Gson().toJson(configResult)
        val oldUserInfoStr = MMKV.defaultMMKV().decodeString(SP_CONFIG, "") ?: ""
        if (newUserInfoStr == oldUserInfoStr) {
            return
        }
        MMKV.defaultMMKV().encode(SP_CONFIG, newUserInfoStr)
    }

    /**获取配置信息**/
    fun getConfig(): SysConfigResult? {
        val config = MMKV.defaultMMKV().decodeString(SP_CONFIG, "") ?: ""
        return try {
            Gson().fromJson(config, SysConfigResult::class.java)
        } catch (e: java.lang.Exception) {
            null
        }
    }


    /**保存用户信息**/
    fun saveUserInfo(userInfo: UserBeanData?) {
        if (userInfo == null) {
            return
        }
        val newUserInfoStr = Gson().toJson(userInfo)
        val oldUserInfoStr = MMKV.defaultMMKV().decodeString(SP_USER_INFO, "") ?: ""
        if (newUserInfoStr == oldUserInfoStr) {
            return
        }
        MMKV.defaultMMKV().encode(SP_USER_INFO, newUserInfoStr)
    }

    /**获取用户信息**/
    fun getUserInfo(): UserBeanData? {
        val userInfoStr = MMKV.defaultMMKV().decodeString(SP_USER_INFO, "") ?: ""
        return try {
            Gson().fromJson(userInfoStr, UserBeanData::class.java)
        } catch (e: java.lang.Exception) {
            null
        }
    }


    /**保存用户信息 积分模块 **/
    fun saveIntegralUserInfo(userInfo: IntegralUserInfoData?) {
        if (userInfo == null) {
            return
        }
        val newUserInfoStr = Gson().toJson(userInfo)
        val oldUserInfoStr = MMKV.defaultMMKV().decodeString(SP_USER_INFO_IN, "") ?: ""
        if (newUserInfoStr == oldUserInfoStr) {
            return
        }
        MMKV.defaultMMKV().encode(SP_USER_INFO_IN, newUserInfoStr)
    }

    /**获取用户信息 积分模块 **/
    fun getIntegralUserInfo(): IntegralUserInfoData? {
        val userInfoStr = MMKV.defaultMMKV().decodeString(SP_USER_INFO_IN, "") ?: ""
        return try {
            Gson().fromJson(userInfoStr, IntegralUserInfoData::class.java)
        } catch (e: java.lang.Exception) {
            null
        }
    }




}